package net.dipper.tankwar.game.control;

import java.util.ArrayList;

import net.dipper.tankwar.core.data.Bullet;
import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.game.helper.Message;
import net.dipper.tankwar.game.helper.MessageManager;
import wnb.util.control.Command;

public class RunBulletCommand extends Command {
	private ObjectMap map;

	public RunBulletCommand(ObjectMap map){
		this.map = map;
	}

	@Override
	public void exec() {
		ArrayList<Bullet> bullets = map.getBullets();
		ArrayList<Bullet> removes = new ArrayList<Bullet>();
		for(Bullet bullet : bullets){
			bullet.step(0.5);
			
			//判断生命值
			double offsetX = bullet.getOriginalX() - bullet.getPosX();
			double offsetY = bullet.getOriginalY() - bullet.getPosY();
			double distance = bullet.getDistance();
			if(offsetX * offsetX + offsetY * offsetY >= distance * distance){
				removes.add(bullet);
			}
			
			//判断是否击中物体
			int x = (int) bullet.getPosX();
			int y = (int)bullet.getPosY();
			if(map.getObject(x, y) > 0){
				MessageManager manager = MessageManager.getInstance();
				map.decreaseHealth(x, y, bullet.getValue());
				manager.push(new Message(Message.HEALTH_DECREASE, x, y, 5));
				removes.add(bullet);
			}
		}
		
		map.removeBullets(removes);
	}

}
